package com.gnerv.battle.boot.security.tool;

import com.gnerv.battle.boot.security.model.LoginAccountDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.session.SessionAuthenticationException;
import org.springframework.util.ObjectUtils;

/**
 * @author Gnerv LiGen
 */
public class SecurityTools {

    private SecurityTools() {
    }

    public static String username() {
        return accountDetails().getUsername();
    }

    public static LoginAccountDetails accountDetails() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (ObjectUtils.isEmpty(authentication)) {
            throw new SessionAuthenticationException("");
        }
        return (LoginAccountDetails) authentication.getPrincipal();
    }

}
